<import resource="/Company Home/Data Dictionary/Scripts/json2.js">

var query = "+TYPE:\"cm:content\"";

var searchTerm = args.keywords;
var searchType = args.type;
var searchIn = args.searchin;

var searchQuery = "";

var searchOkay = false;

if (searchTerm != null && searchTerm.length > 0) {
	searchOkay = true;
	query = query + " +(TEXT:\""+searchTerm+"\")";
	/*var queryLogic = "";
	switch (searchType) {
		// For "ALL" terms, need to have the logic of having ALL entered words in the result
		case "all": {
			queryLogic = "+";
			break;
		}
		// for "ANY", let alfresco's parser handle it
		case "any": 
		default:  {
			break;
		}
	}

	switch (searchIn) {
		case "title": {
			var words = searchTerm.split(/\b/g);
			for (var i in words) {
				var word = words[i];
				if (/[a-zA-Z0-9]+/.test(word)) {
					searchQuery += " "+queryLogic+"@cm\\:title:\""+word+"\"";
				}
			}
			break;
		}
		default: 
			var words = searchTerm.split(/\b/g);
			for (var i in words) {
				var word = words[i];
				if (/[a-zA-Z0-9]+/.test(word)) {
					searchQuery += " "+queryLogic+"(@cm\\:title:\""+word+"\" TEXT:\""+word+"\")";
				}
			}
			break;
	}*/
	
	
}


if (!searchOkay) {
	status.code = 400;
	status.message = "Search term has not been provided.";
	status.redirect = true;
} else {
	var nodes = search.luceneSearch(query);
	
	var results = [];
	for (var i = 0; i < nodes.length; i++) {
		var node = nodes[i];
		var item = {
			id: 'ID WILL BE HERE',
			name: node.properties['cm:name'],
			title: node.properties['cm:title'],
			description: node.properties['cm:description'],
			tags: node.properties['caas:tags']
		};
		
		results[results.length] = item;
	}	
	var jsonString = JSON.stringify(results);
	
	model.callback = args.callback;
	model.context = args.context;
	model.output = jsonString;
}